package cqie.personal.frame.controller;


import cqie.personal.frame.bo.GetMenuBO;
import cqie.personal.frame.entity.MenuInfo;
import cqie.personal.frame.entity.UserInfo;
import cqie.personal.frame.enums.Code;
import cqie.personal.frame.exception.ValidationFailException;
import cqie.personal.frame.service.MenuInfoService;

import cqie.personal.frame.util.JwtUtil;
import cqie.personal.frame.vo.MenuView;
import cqie.personal.frame.vo.PageVO;
import cqie.personal.frame.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yeeeeee
 * @since 2021-09-03
 */
@RestController
@RequestMapping("/menu-info")
@Api(tags = "菜单公共访问接口")
public class MenuInfoController {

    @Resource
    private MenuInfoService menuInfoService;

    @ApiOperation("条件查询菜单")
    @GetMapping("/getMenu")
    public ResultVO getMenuInfo(GetMenuBO getMenuBO) throws ValidationFailException {
        PageVO<List<MenuInfo>> result = menuInfoService.getMenu(getMenuBO);
        if(result!=null){
            if(result.getRecordCount()>0){
                return new ResultVO(result);
            }
            return new ResultVO(Code.NULL_DATA.getCode(),Code.NULL_DATA.getMessage());
        }
        return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
    }

    @ApiOperation("获取树型菜单")
    @GetMapping("/getTreeMenu")
    public ResultVO getTreeMenu(){
      List<List<MenuView>> result =menuInfoService.getTreeMenu();
        if(result!=null){
            if(result.size()>0){
                return new ResultVO(result);
            }
            return new ResultVO(Code.NULL_DATA.getCode(),Code.NULL_DATA.getMessage());
        }
        return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
    }

    @ApiOperation("获取树型菜单2")
    @GetMapping("/getTreeMenu2")
    public ResultVO getTreeMenu2(){
        List<MenuView> result =menuInfoService.getTreeMenu2();
        if(result!=null){
            if(result.size()>0){
                return new ResultVO(result);
            }
            return new ResultVO(Code.NULL_DATA.getCode(),Code.NULL_DATA.getMessage());
        }
        return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
    }

    @ApiOperation("获取用户菜单")
    @GetMapping("/getUserMenu")
    public ResultVO getUserMenu(Integer uid , HttpServletRequest request) throws ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,uid)){
            List<MenuView> result = menuInfoService.getUserMenu(uid);
            if(result.size()>0){
                return new ResultVO(result);
            }
            return new ResultVO(Code.NULL_DATA.getCode(),Code.NULL_DATA.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    private boolean userTokenValid(String token , Integer uid){
        UserInfo user = JwtUtil.parsingToken(token);
        return user.getUid().equals(uid);
    }
}

